﻿using System;
using System.Configuration;
using System.Xml;
using Backend.JYD.Messages.Exceptions;

namespace Backend.JYD.Config.Watcher
{
    public class WatcherConfigurationSectionHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
            if (section == null)
                return null;
            //    throw new ArgumentNullException("没有找到文件监视器配置节点 : \"" + ConfigUtilities.ConfigName_WatcherConfig + "\"");

            XmlNode wacthers = section.SelectSingleNode(ConfigUtilities.ConfigName_WatcherConfig_Wacthers);
            if (wacthers == null)
                throw new ConfigMessageException(Messages.Manager.ConfigMessages.Messages.CannotFindTheSectionName, ConfigUtilities.ConfigName_WatcherConfig_Wacthers);
            if (wacthers.ChildNodes == null || wacthers.ChildNodes.Count <= 0)
                throw new ConfigMessageException(Messages.Manager.ConfigMessages.Messages.TheSectionHasnotChildNodes, ConfigUtilities.ConfigName_WatcherConfig_Wacthers);

            XmlNodeList nodes = wacthers.ChildNodes;
            foreach (XmlNode node in nodes)
            {
                try
                {
                    WatcherConfig config = new WatcherConfig();

                    config.Name = node.Attributes[ConfigUtilities.ConfigName_Config_Name].Value;
                    if (string.IsNullOrEmpty(config.Name))
                        throw new ConfigMessageException(Messages.Manager.ConfigMessages.Messages.CannotFindTheAttributeName, ConfigUtilities.ConfigName_WatcherConfig_Wacthers, ConfigUtilities.ConfigName_Config_Name);

                    string type = node.Attributes[ConfigUtilities.ConfigName_WatcherConfig_Type].Value;
                    if (string.IsNullOrEmpty(type))
                        throw new ConfigMessageException(Messages.Manager.ConfigMessages.Messages.CannotFindTheAttributeName, ConfigUtilities.ConfigName_WatcherConfig_Wacthers, ConfigUtilities.ConfigName_WatcherConfig_Type);
                    if (!Enum.IsDefined(typeof(Watcher.WatcherConfig.TargetType), type))
                        throw new ConfigMessageException(Messages.Manager.ConfigMessages.Messages.TheAttributeValueIsError, ConfigUtilities.ConfigName_WatcherConfig_Type);
                    config.WatcherType = (Watcher.WatcherConfig.TargetType)Enum.Parse(typeof(Watcher.WatcherConfig.TargetType), type);

                    string path = node.Attributes[ConfigUtilities.ConfigName_WatcherConfig_Path].Value;
                    if (string.IsNullOrEmpty(path))
                        throw new ConfigMessageException(Messages.Manager.ConfigMessages.Messages.CannotFindTheAttributeName, ConfigUtilities.ConfigName_WatcherConfig_Wacthers, ConfigUtilities.ConfigName_WatcherConfig_Path);
                    if (config.WatcherType == WatcherConfig.TargetType.Folder)
                    {
                        config.Path = AppDomain.CurrentDomain.BaseDirectory+ path;
                    }
                    else
                    {
                        int idx = path.LastIndexOf('\\');
                        config.Path = path.Substring(0, idx) + "\\";
                        config.FileName = path.Substring(idx + 1);
                    }
                    string enable = node.Attributes[ConfigUtilities.ConfigName_Config_Enable].Value;
                    config.Enable = (enable == null || enable.ToLower() == "true");

                    config.IncludeSubdirectories = false;
                    if (config.WatcherType == WatcherConfig.TargetType.Folder)
                    {
                        string include = node.Attributes[ConfigUtilities.ConfigName_WatcherConfig_IncludeSubdirectories].Value;
                        if (include != null && include.ToLower() == "true") config.IncludeSubdirectories = true;
                    }

                    config.Executer = node.Attributes[ConfigUtilities.ConfigName_WatcherConfig_Executer].Value;
                    if (string.IsNullOrEmpty(config.Executer))
                        throw new ConfigMessageException(Messages.Manager.ConfigMessages.Messages.CannotFindTheAttributeName, ConfigUtilities.ConfigName_WatcherConfig_Wacthers, ConfigUtilities.ConfigName_WatcherConfig_Executer);

                    AppConfigs<WatcherConfig>.Instance.AddConfig(config);
                }
                catch (Exception ex)
                {
                    throw new ConfigMessageException(Messages.Manager.ConfigMessages.Messages.TheSectionConfigError, ex, ConfigUtilities.ConfigName_WatcherConfig);
                }
            }

            return AppConfigs<WatcherConfig>.Instance;
        }
    }
}
